package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Connect_Request类
 * 实现连接请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Connect_Request implements AxdrType {

    private byte[] dataCode = null;
    private PIID piid = null;
    private Unsigned16 wver = null;
    private ProtocolConformance wpro = null;
    private FunctionConformance wfun = null;
    private Unsigned16 wsendsize = null;
    private Unsigned16 wrecvsize = null;
    private Unsigned8 wrecvwnd = null;
    private Unsigned16 wmaxapdu = null;
    private Unsigned32 wmaxidle = null;
    private ConnectMechanismInfo connectMechanismInfo = null;

    public Connect_Request() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid, Unsigned16 wver, ProtocolConformance wpro, FunctionConformance wfun,
            Unsigned16 wsendsize, Unsigned16 wrecvsize, Unsigned8 wrecvwnd, Unsigned16 wmaxapdu,
            Unsigned32 wmaxidle, ConnectMechanismInfo connectMechanismInfo) {
        this.piid = piid;
        this.wver = wver;
        this.wpro = wpro;
        this.wfun = wfun;
        this.wsendsize = wsendsize;
        this.wrecvsize = wrecvsize;
        this.wrecvwnd = wrecvwnd;
        this.wmaxapdu = wmaxapdu;
        this.wmaxidle = wmaxidle;
        this.connectMechanismInfo = connectMechanismInfo;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);

        this.wver = new Unsigned16();
        codeLength += this.wver.decode(input);

        this.wpro = new ProtocolConformance();
        codeLength += this.wpro.decode(input);

        this.wfun = new FunctionConformance();
        codeLength += this.wfun.decode(input);

        this.wsendsize = new Unsigned16();
        codeLength += this.wsendsize.decode(input);

        this.wrecvsize = new Unsigned16();
        codeLength += this.wrecvsize.decode(input);

        this.wrecvwnd = new Unsigned8();
        codeLength += this.wrecvwnd.decode(input);

        this.wmaxapdu = new Unsigned16();
        codeLength += this.wmaxapdu.decode(input);

        this.wmaxidle = new Unsigned32();
        codeLength += this.wmaxidle.decode(input);

        this.connectMechanismInfo = new ConnectMechanismInfo();
        codeLength += this.connectMechanismInfo.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.piid != null && this.wver != null && this.wpro != null && this.wfun != null &&
                    this.wsendsize != null && this.wrecvsize != null && this.wrecvwnd != null &&
                    this.wmaxapdu != null && this.wmaxidle != null && this.connectMechanismInfo != null) {
                codeLength = 0;
                codeLength += this.connectMechanismInfo.encode(output);
                codeLength += this.wmaxidle.encode(output);
                codeLength += this.wmaxapdu.encode(output);
                codeLength += this.wrecvwnd.encode(output);
                codeLength += this.wrecvsize.encode(output);
                codeLength += this.wsendsize.encode(output);
                codeLength += this.wfun.encode(output);
                codeLength += this.wpro.encode(output);
                codeLength += this.wver.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("One or more fields are null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            // Handle exception
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.piid != null && this.wver != null && this.wpro != null && this.wfun != null &&
                this.wsendsize != null && this.wrecvsize != null && this.wrecvwnd != null &&
                this.wmaxapdu != null && this.wmaxidle != null && this.connectMechanismInfo != null) {
            return "sequence: {" +
                    "piid: " + this.piid +
                    ", wver: " + this.wver +
                    ", wpro: " + this.wpro +
                    ", wfun: " + this.wfun +
                    ", wsendsize: " + this.wsendsize +
                    ", wrecvsize: " + this.wrecvsize +
                    ", wrecvwnd: " + this.wrecvwnd +
                    ", wmaxapdu: " + this.wmaxapdu +
                    ", wmaxidle: " + this.wmaxidle +
                    ", connectMechanismInfo: " + this.connectMechanismInfo +
                    "}";
        } else {
            return "One or more fields are null";
        }
    }

    public PIID getPiid() {
        return piid;
    }

    public Unsigned16 getWver() {
        return wver;
    }

    public ProtocolConformance getWpro() {
        return wpro;
    }

    public FunctionConformance getWfun() {
        return wfun;
    }

    public Unsigned16 getWsendsize() {
        return wsendsize;
    }

    public Unsigned16 getWrecvsize() {
        return wrecvsize;
    }

    public Unsigned8 getWrecvwnd() {
        return wrecvwnd;
    }

    public Unsigned16 getWmaxapdu() {
        return wmaxapdu;
    }

    public Unsigned32 getWmaxidle() {
        return wmaxidle;
    }

    public ConnectMechanismInfo getConnectMechanismInfo() {
        return connectMechanismInfo;
    }
}